from rest_framework.permissions import BasePermission
from storage.models import User, File, Access
from rest_framework import exceptions
class IsOwner(BasePermission):
    def has_permission(self, request, view):
        if not (request.user and request.user.is_authenticated):
            return False

        file_id = view.kwargs['file_id']

        if not File.objects.filter(file_id=file_id).exists():
            raise exceptions.NotFound()

        file = File.objects.filter(file_id=file_id).first()

        if Access.objects.filter(user=request.user, file=file, isOwner=True).exists():
            return True

        return False


class IsAccess(BasePermission):
    def has_permission(self, request, view):
        if not (request.user and request.user.is_authenticated):
            return False

        file_id = view.kwargs['file_id']

        if not File.objects.filter(file_id=file_id).exists():
            raise exceptions.NotFound()

        file = File.objects.filter(file_id=file_id).first()

        if Access.objects.filter(user=request.user, file=file).exists():
            return True

        return False